package DB;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * Database connection manager class, which is implemented as a singleton
 * @author Selemani
 */
public class DBConnector {

    private static final String DRIVER = "jdbc:sqlserver://balder.ucn.dk:1433";
    private static final String DATABASENAME = ";databaseName=DM73_4";
    private static final String USERNAME = ";user=DM73_4";
    private static final String PASSWORD = ";password=MaaGodt";
    private static final String DRIVERLOC = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    private static Connection connection;
    private static DBConnector instance = null;

    private DBConnector() {
        String url = DRIVER + DATABASENAME + USERNAME + PASSWORD;

        try {
            Class.forName(DRIVERLOC);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

        try {
            connection = DriverManager.getConnection(url);
            connection.setAutoCommit(true);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static void closeConnection() {
        try {
            connection.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Connection getDBConnection() {
        return connection;
    }

    public static DBConnector getInstance() {
        if (instance == null) {
            instance = new DBConnector();
        }
        return instance;
    }
}
